<?php
 
 error_reporting(E_ALL);
 ini_set("display_errors", 1);
 
 include_once "../common/base.php";
 
class fetcher
{
    private $_db;
	private $log_level;
	
    public function __construct($db=NULL)
    {
        if(is_object($db))
        {
            $this->_db = $db;
        }
        else
        {
            $dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME;
            $this->_db = new PDO($dsn, DB_USER, DB_PASS);
        }
    }
	
	public function init(){
        $this->log_level = 4;
	}
	
	//FIXME number of mails VS user limit
	public function getNumberOfMailsToSend(){
		$hour_diff = 0;
		$mail_count = 0;
		$sql = "SELECT Hour(TIMEDIFF(start,CURRENT_TIMESTAMP)) as hour_diff
                FROM stat";
        if($stmt = $this->_db->prepare($sql)) {
            $stmt->execute();
			$row = $stmt->fetch();
			$hour_diff = $row['hour_diff'];
		}
		else
		{
        	echo "Error! Sorry, Error in SQL.";
        }
		//if($hour_diff == 0) return 0;
		
		if($hour_diff % 24 == 0){
			$sql = "UPDATE gmail set count=0";
			if($stmt = $this->_db->prepare($sql)) {
				$stmt->execute();
			}
			$sql = "UPDATE yahoo set count=0";
			if($stmt = $this->_db->prepare($sql)) {
				$stmt->execute();
			}
			$sql = "UPDATE takethemail set count=0";
			if($stmt = $this->_db->prepare($sql)) {
				$stmt->execute();
			}
			$sql = "UPDATE hotmail set count=0";
			if($stmt = $this->_db->prepare($sql)) {
				$stmt->execute();
			}
		}
		
		$current_day = floor($hour_diff / 24);
		
		echo "current day:" . $current_day;
		
		if($current_day > 41) $current_day = 41;
		
		$sql = "SELECT count
                FROM schedule where day=:current_day";
        if($stmt = $this->_db->prepare($sql)) {
			$stmt->bindParam(":current_day", $current_day, PDO::PARAM_INT);
            $stmt->execute();
			$row = $stmt->fetch();
			$mail_count = $row['count'];
		}
		else
		{
        	echo "Error! Sorry, Error in SQL.";
        }
		echo "count:" . $mail_count;
		return $mail_count;
	}
	
	public function getBestFrom(){
		$sender = "";
		$sql = "SELECT mailid
                FROM takethemail ORDER BY count LIMIT 1";
        if($stmt = $this->_db->prepare($sql)) {
            $stmt->execute();
    		$row = $stmt->fetch();
			if(isset($row['mailid'])){
				$sender = $row['mailid'];
				$sql = "UPDATE takethemail SET count=count+1 WHERE mailid=:mailid";
				if($stmt = $this->_db->prepare($sql)) {
					$stmt->bindParam(":mailid", $sender, PDO::PARAM_STR);
		            $stmt->execute();
				}
				return $sender;
			}
			else return "";
		}
		else
		{
			return "";
        }
	}
	
	public function getBestTo(){
		$sender = array();
		
		$sql = "SELECT mailid
                FROM gmail where count<200 ORDER BY count LIMIT 1";
        if($stmt = $this->_db->prepare($sql)) {
            $stmt->execute();
    		$row = $stmt->fetch();
			if(isset($row['mailid'])){
				$sender['gmail'] = $row['mailid'];
				$sql = "UPDATE gmail SET count=count+1 WHERE mailid=:mailid";
				if($stmt = $this->_db->prepare($sql)) {
					$stmt->bindParam(":mailid", $sender['gmail'], PDO::PARAM_STR);
		            $stmt->execute();
				}
			}
		}
		
		/*$sql = "SELECT mailid
                FROM hotmail where count<200 ORDER BY count LIMIT 1";
        if($stmt = $this->_db->prepare($sql)) {
            $stmt->execute();
    		$row = $stmt->fetch();
			if(isset($row['mailid'])){
				$sender['hotmail'] = $row['mailid'];
				$sql = "UPDATE hotmail SET count=count+1 WHERE mailid=:mailid";
				if($stmt = $this->_db->prepare($sql)) {
					$stmt->bindParam(":mailid", $sender['hotmail'], PDO::PARAM_STR);
		            $stmt->execute();
				}
			}
		}*/
		
		/*
		$sql = "SELECT mailid
                FROM yahoo where count<200 ORDER BY count LIMIT 1";
        if($stmt = $this->_db->prepare($sql)) {
            $stmt->execute();
    		$row = $stmt->fetch();
			if(isset($row['mailid'])){
				$sender['yahoo'] = $row['mailid'];
				$sql = "UPDATE yahoo SET count=count+1 WHERE mailid=:mailid";
				if($stmt = $this->_db->prepare($sql)) {
					$stmt->bindParam(":mailid", $sender['yahoo'], PDO::PARAM_STR);
		            $stmt->execute();
				}
			}
		}
		*/
		
		return $sender;
		
	}
   	
	
   public function doJob(){
	   echo "do job";
	   $mail_count = $this->getNumberOfMailsToSend();
	   
	   for($i=0;$i<$mail_count;$i+=1){
			$from = $this->getbestFrom();
			$to = $this->getBestTo();
			
			$from_interval = 2;
			$to_interval = 10;
			$from_time = date("Y-m-d H:i:s", strtotime("+{$from_interval} minutes", time()));
			/*$sql = "INSERT into send_mail values
				(:from, :gmail, :from_time),
				(:from, :hotmail, :from_time),
				(:from, :yahoo, :from_time),
				(:gmail, :from, :to_time),
				(:hotmail, :from, :to_time),
				(:yahoo, :from, :to_time)
				;";
			*/	
			$sql = "INSERT into send_mail values
				(:from, :gmail, :from_time, NULL, 0)
				;";


			
			if(isset($to['gmail'])){
				if($stmt = $this->_db->prepare($sql)) {
					$stmt->bindParam(":from", $from, PDO::PARAM_STR);
					$stmt->bindParam(":gmail", $to['gmail'], PDO::PARAM_STR);
					//$stmt->bindParam(":hotmail", $to['hotmail'], PDO::PARAM_STR);
					//$stmt->bindParam(":yahoo", $to['yahoo'], PDO::PARAM_STR);
					$stmt->bindParam(":from_time", $from_time, PDO::PARAM_STR);
					//$stmt->bindParam(":to_time", $to_time, PDO::PARAM_STR);
					$stmt->execute();
				}
			}
	   }
   }
}
?>